<template>
	<view class="p30">
		<view class="card-shadow-box over-hidden">
			<view class="bold pt30 t-center">近30日{{title}}</view>
			<view class="c-info f20">单位：{{title == '平均油耗' ? 'L/100km':'V'}}</view>
			<cust-line ref="line"  :min="0" :max="16"></cust-line>
		</view>
		<view class="c-info mt40 f20 flex-column-center">
			{{title=="平均油耗"?"日平均油耗：当天油耗量除以当天车辆行驶里程":"电瓶电压取值于车辆未运行状态下，每天最后一次更新的值"}}
		</view>
	</view>
</template>

<script>
	import {
		getVoltageCountAPI,
		getMeanAverageOilCountAPI
	} from "@/api/api/statistics"
	import {
		getNowTime,
		getRangeVal
	} from "@/utils/utils.js"
	import custLine from "@/components/charts/cust-line";

	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			custLine
		},
		data() {
			return {
				lineData: {},
				title: '',
			}
		},
		computed: {
			...mapState(["deviceData"])
		},

		onLoad(params) {
			this.title = params.title;
			uni.setNavigationBarTitle({
				title: params.title
			})
			this.initData();
		},
		methods: {
			initData() {
				let fun = this.title == "平均油耗" ? getMeanAverageOilCountAPI : getVoltageCountAPI;
				let params = {
					carId: this.deviceData.id, //"1625322845474340866",//
					dimension: 2,
					count: 30,
					endTime: getNowTime()
				}
				let sData = [];
				let categories = [];
				fun(params).then(([err, data]) => {
					//data = data.reverse() || [];
					data.forEach((item) => {
						let time = item.time;
						categories.push(time);
						let val = this.toFormatVal(
							item.info,
							this.title == "平均油耗" ? [0, 4.5] : [6, 16],
							1
						);
						sData.push(val)
					})
					
					let lineData = {
						categories: categories,
						series: [{
							name: '',
							data: sData
						}]
					}
					this.$nextTick(() => {
						this.$refs.line.updateData(lineData);
					})
				})
			},

			toFormatVal(val, arr, fix = 2) {
				if (val == null || val === '') {
					return '-'
				}
				let n = getRangeVal(val, arr);
				return Number(n).toFixed(fix);
			}
		}
	}
</script>

<style>
</style>
